<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="门禁名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入门禁名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:door:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="doorList" border stripe>
      <el-table-column label="#" type="index" width="80" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="门禁名称" align="center" prop="name" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="进门" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="baseUrl + scope.row.inletUrl"
            :preview-src-list="[baseUrl + scope.row.inletUrl]"
            fit="contain"
          />
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="downloadImg(scope.row, 'inlet')"
            v-hasPermi="['device:door:download']"
          >
            下载
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="出门" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="baseUrl + scope.row.exportUrl"
            :preview-src-list="[baseUrl + scope.row.exportUrl]"
            fit="contain"
          />
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="downloadImg(scope.row, 'export')"
            v-hasPermi="['device:door:download']"
          >
            下载
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="停车点位" align="center" prop="areaName">
        <template slot-scope="scope">
          <el-dropdown
            trigger="click"
            @command="
              (v) => {
                handleCommand(v, scope.row);
              }
            "
          >
            <span class="el-dropdown-link">
              {{ scope.row.areaName || "绑定" }}
              <i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(i, k) in areaList" :key="k" :command="i.id">
                {{ i.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">
            {{ scope.row.status == 1 ? "正常" : "停用" }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listDoor, updateDoor } from "@/api/device/door";
import { listArea } from "@/api/weiriya/area";

export default {
  name: "Door",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 遮罩层
      ids: [], // 选中数组
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      doorList: [], // 门禁表格数据
      areaList: [], // 停车点位列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        deviceId: null,
        areaId: null,
        openUrl: null,
        closeUrl: null,
      },
    };
  },
  created() {
    this.getAreaList();
    this.getList();
  },
  methods: {
    // 查询停车点位列表
    getAreaList() {
      let data = {
        pageNum: 1,
        pageSize: 1000,
      };
      listArea(data).then((res) => {
        this.areaList = res.rows;
      });
    },
    // 查询门禁列表
    getList() {
      this.loading = true;
      listDoor(this.queryParams)
        .then((res) => {
          this.doorList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 绑定停车点位
    handleCommand(v, row) {
      row.areaId = v;
      this.$modal
        .confirm("确认绑定该停车点位吗？")
        .then(() => {
          updateDoor(row).then(() => {
            this.handleQuery();
            this.$modal.msgSuccess("绑定成功");
          });
        })
        .catch(() => {});
    },
    // 下载图片
    downloadImg(row, type) {
      const a = document.createElement("a");
      a.href = this.baseUrl + row[type + "Url"];
      a.download = row.name + (type == "inlet" ? "进门" : "出门") + ".png";
      a.click();
      a.remove();
    },
    // 导出
    handleExport() {
      this.download(
        "system/door/export",
        {
          ...this.queryParams,
        },
        `door_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>
